package Shopping;

import java.util.Scanner;



public class ShoppingCart {
    static Scanner s = new Scanner(System.in);

    public static void main(String[] args) {
        //定义购物车商品数组和容量
        Goods[] Cart = new Goods[3];
        int capacity = 3;
        //定义商品数组
        Goods[] shop = new Goods[5];
        //创建商品对象
        Goods cola = new Goods("可乐", "饮品", 5, 0);
        Goods juice = new Goods("果汁", "饮品", 3, 0);
        Goods apple = new Goods("苹果", "水果", 3, 0);
        Goods banana = new Goods("香蕉", "水果", 4, 0);
        Goods straw = new Goods("草莓", "水果", 6, 0);
        //将商品存入数组
        shop[0] = cola;
        shop[1] = juice;
        shop[2] = apple;
        shop[3] = banana;
        shop[4] = straw;
        //循环向购物车添加商品，商品数量不超过capacity
        for (int j = 0; j < capacity; j++) {
            System.out.println("请输入您要加入购物车的商品：");
            String input = s.nextLine();
            System.out.println("请输入您加入购物车商品的数量：");
            int incount = s.nextInt();
            s.nextLine();
            //标记是否找到商品
            boolean found = false;
            //遍历商品数组
            for (int i = 0; i < 5; i++) {
                if (input.equals(shop[i].getName())) {
                    //将其添加至购物车数组
                    Cart[j] = shop[i];
                    Cart[j].setQuantity(incount);
                    Cart[j].print();
                    found = true;
                    break;
                }
            }
            //商品不存在
            if (!found) {
                System.out.println("您输入的商品不存在，请重新输入！");
                j--;
                continue;
            }
                    //购物车已满
                    if (j == 2) {
                        System.out.println("购物车已满，输入1结算输入2删除某商品");
                        int option = s.nextInt();
                        s.nextLine();
                        //进行结算
                        if(option == 1){
                            Total(Cart);
                        }
                        if (option == 2) {
                            //定义一个删除的方法
                            Cart=removegoods(Cart);
                            j--;
                            break;
                        }

                    }


        }
    }

    public static Goods[] removegoods(Goods[] goods) {
        System.out.println("请输入要删除的商品");
        String input = s.nextLine();
        for (int i = 0; i < goods.length; i++) {
            if (input.equals(goods[i].getName())) {
                for (int j = i; j < goods.length - 1; j++) {
                    goods[j] = goods[j + 1];
                }
                break;
            }
        }
        return goods;
    }
    public static void Total(Goods[] goods) {
        double total = 0.0;
        for (int i = 0; i < goods.length; i++) {
            Goods good = goods[i];
            // 打印商品信息
            good.print();
            if (good!= null) {
                total += good.getPrice() * good.getQuantity();
            }
        }
        System.out.println("购物车商品总价为: " + total);
    }
    }

